package com.webserver.controller;

import com.webserver.entity.User;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;

/**
 * @author Administrator
 * @Description
 * @date 2022/5/18 16:27
 **/
public class UserController {
    private static File userDir;//表示保存所有用户信息的目录

    static {
        userDir = new File("./users");
        if (!userDir.exists()) {
            userDir.mkdirs();
        }
    }

    /**
     * @param request
     * @param response
     * @author
     * @description 处理用户注册
     * @date 2022/5/18 16:32
     */
    public void reg(HttpServletRequest request, HttpServletResponse response) {
        //获取用户注册信息


        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String age = request.getParameter("age");
        if (username.isEmpty() || password.isEmpty() || nickname.isEmpty() || age.isEmpty() || !age.matches("[0-9]+")){
//            File file = new File(DispatchServlet.staticDir,"/myweb/reg_info_error.html");
//            response.setContentFile(file);
            response.sendRedirect("/myweb/reg_info_error.html");
            return;
        }
        //将用户信息序列化到uses目录中
        User user = new User(username, password, nickname, Integer.parseInt(age));
    try {
        File userFile = new File(userDir, username + ".obj");
        if (userFile.exists()){
//            File file = new File(DispatchServlet.staticDir,"/myweb/reg_already_existed.html");
//            response.setContentFile(file);
            response.sendRedirect("/myweb/reg_already_existed.html");
            return;
        }
        FileOutputStream fos = new FileOutputStream(userFile);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fos);
        objectOutputStream.writeObject(user);
    }catch (IOException e){
        e.printStackTrace();
    }
        //设置response的正文文件为注册结果页面
//        File file = new File(DispatchServlet.staticDir, "/myweb/reg_success.html");
//        response.setContentFile(file);
        response.sendRedirect("/myweb/reg_success.html");
    }
    public void login(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (username.isEmpty() || password.isEmpty()){
//            File file = new File(DispatchServlet.staticDir,"/myweb/login_info_error.html");
//            response.setContentFile(file);
            response.sendRedirect("/myweb/login_info_error.html");
            return;
        }

        File userFile = new File(userDir,username + ".obj");


        if (userFile.exists()){
            try {
                ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(userFile));
                User user = (User) objectInputStream.readObject();
                if (user.getPassword().equals(password)){
                    System.out.println("登录成功");
//                    File resultFile = new File(DispatchServlet.staticDir, "/myweb/login_success.html");
//                    response.setContentFile(resultFile);
                    response.sendRedirect("/myweb/login_success.html");
                    return;
                }
            }catch (IOException | ClassNotFoundException e){
                e.printStackTrace();
            }

        }
            System.out.println("登录失败");
//            File resultFile = new File(DispatchServlet.staticDir, "/myweb/login_info_error.html");
//            response.setContentFile(resultFile);
            response.sendRedirect("/myweb/login_info_error.html");


    }
}
